//
//  UILabel+WDExtention.m
//  MyDemos
//
//  Created by iMac on 2018/3/14.
//  Copyright © 2018年 iMac. All rights reserved.
//

#import "UILabel+WDExtention.h"
#import <objc/runtime.h>


const char *tapLableCallbackKey = "tapLableCallbackKey";
@implementation UILabel (WDExtention)




/// set text
- (UILabel *(^)(NSString *))sText {
    __weak typeof(self)weekSelf = self;
    return ^UILabel *(NSString *text) {
        weekSelf.text = text;
        return weekSelf;
    };
}
/// set color
- (UILabel *(^)(UIColor *))sColor {
    __weak typeof(self)weekSelf = self;
    return ^UILabel *(UIColor *color) {
        weekSelf.textColor = color;
        return weekSelf;
    };
}
/// set text color
- (UILabel *(^)(NSString *, UIColor *))sTextColor {
    __weak typeof(self)weekSelf = self;
    return ^UILabel *(NSString *text, UIColor *color) {
        weekSelf.text = text;
        weekSelf.textColor = color;
        return weekSelf;
    };
}

/// set background color
- (UILabel *(^)(UIColor *))sBGColor {
    __weak typeof(self)weekSelf = self;
    return ^UILabel *(UIColor *color) {
        weekSelf.backgroundColor = color;
        return weekSelf;
    };
}

/// set system font size
- (UILabel *(^)(CGFloat))sSysFont {
    __weak typeof(self)weekSelf = self;
    return ^UILabel *(CGFloat size) {
        weekSelf.font = [UIFont systemFontOfSize:size];
        return weekSelf;
    };
}
/// set font size & fontName
- (UILabel *(^)(CGFloat, NSString *))sFontName {
    __weak typeof(self)weekSelf = self;
    return ^UILabel *(CGFloat size, NSString *name) {
        weekSelf.font = [UIFont fontWithName:name size:size];
        return weekSelf;
    };
}

/// set attribute text
- (UILabel *(^)(NSAttributedString *))sAttrText {
    __weak typeof(self)weekSelf = self;
    return ^UILabel *(NSAttributedString *attrStr) {
        weekSelf.attributedText = attrStr;
        return weekSelf;
    };
}

/// frame
- (UILabel *(^)(CGRect))sFrame {
    __weak typeof(self)weekSelf = self;
    return ^UILabel *(CGRect frame) {
        weekSelf.frame = frame;
        return weekSelf;
    };
}

/// numberOfline
- (UILabel *(^)(NSInteger))sLineNum {
    __weak typeof(self)weekSelf = self;
    return ^UILabel *(NSInteger num) {
        weekSelf.numberOfLines = num;
        return weekSelf;
    };
}

/// textAlignment
- (UILabel *(^)(NSTextAlignment))sAlignment {
    __weak typeof(self)weekSelf = self;
    return ^UILabel *(NSTextAlignment alignment) {
        weekSelf.textAlignment = alignment;
        return weekSelf;
    };
}

/// tap callback
- (void)tap:(void(^)(UILabel *))callback {
    self.tapCallback = callback;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    [tap addTarget:self action:@selector(lableClick:)];
    [self addGestureRecognizer:tap];
}
- (void)lableClick:(UILabel *)sender {
    if (self.tapCallback) {
        self.tapCallback(sender);
    }
}

- (void)setTapCallback:(void (^)(UILabel *))tapCallback {
    objc_setAssociatedObject(self, tapLableCallbackKey, tapCallback, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (void (^)(UILabel *))tapCallback {
    return objc_getAssociatedObject(self, tapLableCallbackKey);
}

@end
